package com.coolgor.axnut.service.airuc.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.airuc.CourseDao;
import com.coolgor.axnut.dto.airuc.CourseExecution;
import com.coolgor.axnut.entity.airuc.Course;
import com.coolgor.axnut.enums.airuc.CourseStateEnum;
import com.coolgor.axnut.exceptions.airuc.CourseOperationException;
import com.coolgor.axnut.service.airuc.CourseService;
import com.coolgor.axnut.util.PageCalculator;
import com.coolgor.axnut.service.airuc.CourseService;

@Service
public class CourseServiceImpl implements CourseService {
	private static Logger log = LoggerFactory.getLogger(CourseServiceImpl.class);
	
	@Autowired
	private CourseDao courseDao;
	
	
	@Override
	public Course getCourse(Long courseID) {
		return courseDao.queryCourseById(courseID);
	}

	@Override
	public CourseExecution getCourseList(Course courseCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Course> courseList = courseDao.queryCourseList(courseCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = courseDao.queryCourseCount(courseCondition);
		CourseExecution result = new CourseExecution();
		if (courseList != null) {
			result.setState(CourseStateEnum.SUCCESS.getState());
			result.setStateInfo(CourseStateEnum.SUCCESS.getStateInfo());
			result.setCourseList(courseList);
			result.setCount(count);
		} else {
			result.setState(CourseStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public CourseExecution addCourse(Course course) {
		// 空值判断
		if (course == null) {
			return new CourseExecution(CourseStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 赋初始值
			course.setCreateTime(new Date());
			course.setLastEditTime(new Date());
			// 添加
			effectedNum = courseDao.insertCourse(course);
			if (effectedNum <= 0) {
				throw new CourseOperationException("专业信息创建失败");
			} 
		} catch (Exception e) {
			throw new CourseOperationException("addCourse error:" + e.getMessage());
		}
		return new CourseExecution(CourseStateEnum.SUCCESS, course);
	}

	@Override
	@Transactional
	public CourseExecution modifyCourse(Course course) {
		// 空值判断
		if (course == null) {
			return new CourseExecution(CourseStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 赋初始值
			course.setLastEditTime(new Date());
			// 更新操作
			effectedNum = courseDao.updateCourse(course);
			if (effectedNum <= 0) {
				throw new CourseOperationException("专业信息更新失败");
			} 
		} catch (Exception e) {
			throw new CourseOperationException("Modify Course Information error:" + e.getMessage());
		}
		return new CourseExecution(CourseStateEnum.SUCCESS, course);
	}

	@Override
	@Transactional
	public CourseExecution removeCourse(Course course) {
		// 空值判断
		if (course == null) {
			return new CourseExecution(CourseStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = courseDao.deleteCourse(course);
			if (effectedNum <= 0) {
				throw new CourseOperationException("专业信息删除失败");
			} 
		} catch (Exception e) {
			throw new CourseOperationException("Delete Course Information error:" + e.getMessage());
		}
		return new CourseExecution(CourseStateEnum.SUCCESS, course);
	}

	@Override
	@Transactional
	public CourseExecution batchRemoveCourse(List<Course> courseList) {
		// 空值判断
		if (courseList == null) {
			return new CourseExecution(CourseStateEnum.NULL);
		}
		List<Long> courseIDList = new ArrayList<Long>();
		for (int i=0; i<courseList.size(); i++) {
			courseIDList.add(courseList.get(i).getCourseID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = courseDao.batchDeleteCourse(courseIDList);
			if (effectedNum <= 0) {
				throw new CourseOperationException("专业信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new CourseOperationException("Batch delete Course Information error:" + e.getMessage());
		}
		CourseExecution ce = new CourseExecution(CourseStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		ce.setCount(effectedNum);
		return ce;
	}

}
